package servlet;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.util.Bytes;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/login")
public class Login extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if ("/login".equals(servletPath))
        {
         doLogin(request,response);
        }
    }
    public Integer rowCount(String tablename){
        Integer rowCount = 0;
        try {
            //  创建HBase配置对象
            Configuration configuration = HBaseConfiguration.create();
            // 指定zookeeper集群地址
            configuration.set("hbase.zookeeper.quorum", "192.168.88.11");
            // 创建连接对象
            Connection connection = ConnectionFactory.createConnection(configuration);
            // 获取Table对象,Table负责与记录相关的操作,如增删改查等
            Table table = connection.getTable(TableName.valueOf("administ"));
            Scan scan = new Scan();
            //FirstKeyOnlyFilter只会取得每行数据的第一个kv，提升count速度
            scan.setFilter(new FirstKeyOnlyFilter());

            ResultScanner rs = table.getScanner(scan);
            for (Result result : rs) {
                rowCount ++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return rowCount;
    }

    private void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int i;
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //  创建HBase配置对象
        Configuration configuration = HBaseConfiguration.create();
        // 指定zookeeper集群地址
        configuration.set("hbase.zookeeper.quorum", "192.168.88.11");
        // 创建连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);
        // 获取Table对象,Table负责与记录相关的操作,如增删改查等
        Table table = connection.getTable(TableName.valueOf("administ"));
        // 创建GET对象,根据rowKey查询,rowKey=1
//int count = rowCount("administ");
      //  for (i=1; i<= count; i++) {
        Scan scan = new Scan();

          //  Get get = new Get(Bytes.toBytes(i));
            ResultScanner result = table.getScanner(scan);

            for (Result rs:result){
                String u = null;
                String p = null;
                for (KeyValue kv:rs.raw()){

                    if (new String(kv.getQualifier()).equals("username")){
                        u = new String(kv.getValue());

                    }else if (new String(kv.getQualifier()).equals("password")){
                        p = new String(kv.getValue());
                    }

                }

                if (u.equals(username)&&p.equals(password))
                {
                    request.getRequestDispatcher("/admin9.jsp").forward(request,response);

                }
                else

                {
                    PrintWriter out =response.getWriter();
//                    out.print("<script>alert('用户名或密码错误，请重试')</script>");
                    out.print("<alert>用户名或密码错误，请重试</alert>");
//                    request.setAttribute("flag",1);
                    request.getRequestDispatcher("/login9.jsp").forward(request,response);

                }
            }

        // 查询数据,取得结果
        connection.close();
        }
      //}
    }


